package controller;

import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import pagemodel.IndexfilmModel;
import pagemodel.MSG;
import po.Film;
import service.FilmService;
import service.IndexFilmService;

import java.util.List;

@Controller
public class indexfilmController {

    @Autowired
    IndexFilmService indexFilmService;
    @Autowired
    FilmService filmService;


    @RequestMapping("/index")
    public String showindex(){
        return  "/html/index.html";
    }


    @ApiOperation("获取所有电影的名称和图片信息")
    @RequestMapping("/getfilmimg")
    @ResponseBody
        public IndexfilmModel getfilminfo(){
        List<Film> films = indexFilmService.getindexfilminfo();
        IndexfilmModel indexfilmModel= new IndexfilmModel();
        indexfilmModel.setFilms(films);
        indexfilmModel.setSize(films.size());
        return  indexfilmModel;
    }

/*
* key:imdburl
* 以为是根据getFilmsByCondition 而且condition写死为imdbUrl
* 所以返回的只有一部电影
*
* */
    @ApiOperation("首页点击图片获取一部电影的详细信息")
    @RequestMapping("/indexgetfilminfo")
    @ResponseBody
    public MSG indexgetfilminfo(@RequestParam("key")String imdburl){
       List<Film> films= filmService.getFilmsByCondition("imdbUrl",imdburl);
       String introduction = filmService.getFilmIntroductionByImdburl(imdburl);
       Film film = films.get(0);
        String imgurl =  indexFilmService.getfilmImgByImdburl(imdburl);
        film.setImgurl(imgurl);

       film.setIntroduction(introduction);
       MSG msg = new MSG("200",film);
       return  msg;
    }

    @ApiOperation("首页根据条件查询电影")
    @RequestMapping("/indexGetCondition")
    @ResponseBody
    public IndexfilmModel indexGetCondition(@RequestParam("condition") String  condition,
                                 @RequestParam("keyword") String keyword){
        IndexfilmModel indexfilmModel= new IndexfilmModel();
        List<Film> films = filmService.getFilmsByCondition(condition,keyword);
        indexfilmModel.setFilms(films);
        indexfilmModel.setSize(films.size());
        return indexfilmModel;
    }





}
